'use strict';
const { Service } = require('egg');

class FilesService extends Service {
  async checkScope() {
    const { ctx } = this;
    const scopeList = await ctx.service.authorization.getCurrentScope();
    return scopeList.indexOf('file') !== -1;
  }

  async checkFolder(ctx, folder) {
    const user = await ctx.service.authorization.getCurrentUser();

    const folderModel = await ctx.model.File.findByPk(folder);
    if (!folderModel) {
      ctx.throw(404, 'not_found');
      throw { status: 404 };
    }
    if (folderModel.user !== user) {
      ctx.throw(403, 'forbidden');
      throw { status: 403 };
    }
  }
}

module.exports = FilesService;
